package com.itheima.youdianda.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.dto.SysRoleDto;
import com.itheima.youdianda.entity.SysMenu;
import com.itheima.youdianda.entity.SysRole;
import com.itheima.youdianda.entity.SysRoleMenu;
import com.itheima.youdianda.mapper.SysRoleMapper;
import com.itheima.youdianda.mapper.SysRoleMenuMapper;
import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.service.SysRoleMenuService;
import com.itheima.youdianda.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 *
 * 角色表 服务实现类
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleService iSysRoleService;
    @Autowired
    private SysRoleMenuService iSysRoleMenuService;
    @Autowired
    private SysMenuService iSysMenuService;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public void saveWithMenu(SysRoleDto sysRoleDto) {
        //把添加的角色信息保存到role表中
        SysRole sysRole = new SysRole();
        //拷贝
        BeanUtils.copyProperties(sysRoleDto, sysRole, "menuids");
        //因为数据库id不能自增,所以需要自己设置一个id
        sysRole.setId(UUID.randomUUID().toString());
        sysRoleMapper.saveRole(sysRole);
        //获取角色id
        System.out.println(sysRole.getId());
        //把角色权限信息保存到menu表中
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        //设置roleID
        String[] menuids = sysRoleDto.getMenuids();
        //将自己设置一个id传给RoleId
        sysRoleMenu.setRoleId(sysRole.getId());
        log.info("{}",menuids);
        for (String menuid:
                menuids) {
            //设置menuid
            sysRoleMenu.setMenuId(menuid);
            //完成后保存到角色菜单中
            //iSysRoleMenuService.save(sysRoleMenu);
            sysRoleMenuMapper.saveRoleMenu(sysRoleMenu);
        }
    }

    @Override
    public void delWithMenu(String id) {
        //根据id删除role表中对应的数据
        iSysRoleService.removeById(id);

        //根据roleid删除 role_menu中的表
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId, id);
        iSysRoleMenuService.remove(lqw);
    }

    @Override
    public void updateWithMenu(SysRoleDto sysRoleDto) {
        System.out.println(sysRoleDto.toString());
        this.updateById(sysRoleDto);
        //不删除人,只删除他对应的权限.根据roleid删除 role_menu中的表
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId, sysRoleDto.getId());
        iSysRoleMenuService.remove(lqw);
        String[] menuids = sysRoleDto.getMenuids();

        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        for (String menuid : menuids) {
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            if (sysRoleMenu != null) {
                sysRoleMenuMapper.saveRoleMenu(sysRoleMenu);
            } else {
                return;
            }
        }
        /*SysRole sysRole = new SysRole();
        //把dto数据拷给role,忽略掉旧的权限集合,使用待会更新的权限
        BeanUtils.copyProperties(sysRoleDto, sysRole, "menuids");
        String id = sysRole.getId();
        //删除数据
        delWithMenu(id);
        //保存数据
        //将之前逻辑删除的状态变为0;
        sysRoleDto.setDelFlag("0");
        sysRoleMapper.updateById(sysRole);
        String[] menuids = sysRoleDto.getMenuids();

        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        for (String menuid : menuids) {
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenu.setRoleId(id);
            if (sysRoleMenu != null) {
                sysRoleMenuMapper.saveRoleMenu(sysRoleMenu);
            } else {
                return;
            }
        }*/
    }

    @Override
    public SysRoleDto getSysRoleInfo(String id) {

        //找到最初的角色信息
        SysRole sysRole = this.getById(id);
        //准备好要回显的扩展类
        SysRoleDto sysRoleDto = new SysRoleDto();
        //把sysRole的数据拷给Dto
        BeanUtils.copyProperties(sysRole, sysRoleDto);
        //查找SysRoleMenu下权限集合
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId, id);
        List<SysRoleMenu> roleMenuList = iSysRoleMenuService.list(lqw);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < roleMenuList.size(); i++) {
            //得到所有的权限id
            list.add(roleMenuList.get(i).getMenuId());
        }
        //查找Menu表下的权限集合
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysMenu::getId,list);
        List<SysMenu> sysMenuList = iSysMenuService.list(queryWrapper);
        //1.查询根节点数据
        List<SysMenu> rootList = sysMenuList.stream().filter(sysMenu ->
                sysMenu.getPid().equals(0)
        ).collect(Collectors.toList());

        List<SysMenu> sysMenus = rootList.stream().map(root -> {
            root.setChildren(getChildren(root, sysMenuList));
            return root;
        }).collect(Collectors.toList());
        //将得到的权限集合返还给前端回显
        sysRoleDto.setAuthList(sysMenus);
        return sysRoleDto;
    }

    private List<SysMenu> getChildren(SysMenu root, List<SysMenu> sysMenuList) {
        List<SysMenu> childrens = new ArrayList<>();// 子菜单
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPid().equals(root.getId())) {
                childrens.add(sysMenu);
            }
        }
        if (CollectionUtils.isNotEmpty(childrens)) {
            for (SysMenu sysMenu : childrens) {
                List<SysMenu> children_2 = getChildren(sysMenu, sysMenuList);
                sysMenu.setChildren(children_2);
            }
            return childrens;
        }
        return Collections.emptyList();
    }
}